package com.justnow.offer;

/**
 * @author justnow
 * Created on 2020-09-21
 * Description
 */
public class WuBa02 {
    public static void main(String[] args) {
        WuBa02 wuBa02 = new WuBa02();
        int[] ints = wuBa02.countBits(5);
        for (int anInt : ints) {
            System.out.println(anInt);
        }
    }

    public int[] countBits (int num) {
        // write code here
        int[] res = new int[num + 1];
        res[0] = 0;
        for (int i = 1; i < num + 1; i++) {
            res[i] = getOneNumber(i);
        }
        return res;
    }

    public int getOneNumber(int n) {
        int sum = 0;
        while (n > 0) {
            sum++;
            n = n & (n - 1);
        }
        return sum;
    }
}
